package com.bps.zzant.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import com.bps.zzant.model.ToDo;

@Repository
public class ToDoDAOImpl implements ToDoDAO {
	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public List<ToDo> getToDoList(String owner, String status) {
		
		 // List list = getSessionFactory().getCurrentSession().createQuery("from ToDo todo where todo.owner='"+owner +"' and todo.status='"+status+"'").list();
		
		List list = getSessionFactory().getCurrentSession().createCriteria(ToDo.class)
				.add(Restrictions.eq("owner", owner))
				.add(Restrictions.eq("status", status))
				.addOrder(Order.desc("dueDate")).list();
		
		 System.out.println("--------------- the list size:" + list.size());
		return list;
	}

	public void updateToDo(ToDo toDo) {
		System.out.println("--------------- in ToDoDAOImpl.updateToDo");
		System.out.println("--------------- desc:" + toDo.getDesc());
		System.out.println("--------------- toDoId:" +toDo.getId());
		// getSessionFactory().getCurrentSession().update(toDo);
		
		// Query query = getSessionFactory().getCurrentSession().createQuery("update ToDo set desc='deleted' where id = :toDoId");
		Query query = getSessionFactory().getCurrentSession().createSQLQuery(	"update todolist set todo_desc=:toDoDesc, todo_priority=:toDoPriority, "+
																				"owner=:toDoOwner, todo_due=:toDoDueDate, todo_created=now()"+
																				" where todo_id =:toDoId");
		// , priority=:toDoPriority, owner=:toDoOwner, dueDate=:toDoDueDate
		query.setParameter("toDoDesc", toDo.getDesc());
		query.setParameter("toDoPriority", toDo.getPriority());
		query.setParameter("toDoOwner", toDo.getOwner());
		query.setParameter("toDoDueDate", toDo.getDueDate());
		query.setParameter("toDoId", new Integer(toDo.getId()));
		int result = query.executeUpdate();		
		System.out.println("--------------- result is:"+result);
		
	}
	
	public void saveToDo(ToDo toDo) {
		getSessionFactory().getCurrentSession().save(toDo);
	}
	
	public void deleteToDo(ToDo toDo) {
		// TODO Auto-generated method stub
		// getSessionFactory().getCurrentSession().delete(toDo);
		
		Query query = getSessionFactory().getCurrentSession().createQuery("update ToDo set status='deleted', finishDate=current_timestamp() where id = :toDoId");
		query.setParameter("toDoId", new Integer(toDo.getId()));
		int result = query.executeUpdate();		
		System.out.println("--------------- result is:"+result);
	}
	
	public ToDo getToDoById(int id){
		System.out.println("--------------- in the ToDoDAOImpl.getToDoById");
		return (ToDo) getSessionFactory().getCurrentSession().get(ToDo.class, new Integer(id));
	}

	public void finishToDo(ToDo toDo){
		System.out.println("--------------- ToDoDAOImpl.finishToDo:");
		Query query = getSessionFactory().getCurrentSession().createQuery("update ToDo set status='finished', finishDate=current_timestamp() where id = :toDoId");
		query.setParameter("toDoId", new Integer(toDo.getId()));
		int result = query.executeUpdate();		
		
	}
	
	public List<ToDo> searchToDoList(String owner, String status, Date startingDate, Date endDate) {
		List list = getSessionFactory().getCurrentSession().createCriteria(ToDo.class)
				.add(Restrictions.eq("owner", owner))
				.add(Restrictions.eq("status", status))
				.add(Restrictions.between("dueDate", startingDate, endDate))
				.addOrder(Order.desc("dueDate")).list();
		
		 System.out.println("--------------- the list size:" + list.size());
		return list;
	}
	
}
